<script setup>
import { darkTheme, zhCN } from 'naive-ui'
import { useAppStore, useAuthStore } from '@/stores'
import { router } from '@/router'
import Watermark from '@/components/Watermark.vue'

const appStore = useAppStore()
const authStore = useAuthStore()

let timer = null

const resetTimer = () => {
  if (timer) clearTimeout(timer)
  timer = setTimeout(() => {
    authStore.logout() // 注销用户
    router.push('/login') // 跳转到登录页面
  }, 900000) // 超时时间 15 分钟
}

const activityHandler = () => {
  resetTimer() // 重置计时器
}

onMounted(() => {
  window.addEventListener('mousemove', activityHandler)
  window.addEventListener('keypress', activityHandler)
  resetTimer() // 初始化计时器
})

onBeforeUnmount(() => {
  window.removeEventListener('mousemove', activityHandler)
  window.removeEventListener('keypress', activityHandler)
  clearTimeout(timer) // 清除计时器
})
</script>

<template>
  <n-config-provider :locale="zhCN" class="wh-full" inline-theme-disabled :theme="appStore.colorMode === 'dark' ? darkTheme : null" :theme-overrides="appStore.theme">
    <naive-provider>
      <n-message-provider>
        <router-view />
        <Watermark :show-watermark="appStore.showWatermark" />
      </n-message-provider>
    </naive-provider>
  </n-config-provider>
</template>
